package week_1.homework;
//课堂上蓝墨云测试作业。实现链表的几个增、删方法。
public class LinkedlistExample
{
    public static void main(String[] args) {
        Student student1= new Student("康皓越",20172326,"爱打游戏");
        Student student2 = new Student("马瑞蕃",20172327,"爱打篮球");
        Student student3 = new Student("李馨雨",20172328,"爱读诗");
        Student student4 = new Student("王文彬",20172329,"爱学习");
        Student student5 = new Student("何莉",20172334,"爱唱歌");
        Student Head = student2;

        InsertNode(Head,student3);
        InsertNode(Head,student5);
        InsertNode(Head,student3,student5,student4);
        DeleteNode(Head,student5);
        InsertNode1(Head,student1);
        PrintLinkedlist(Head);
    }
    public static void PrintLinkedlist(Student Head){
        Student node = Head;//链表的头不能动。
        while(node!=null){
            System.out.println("姓名："+node.name+"  学号："+node.number+"  爱好："+node.hobby);
            node=node.next;
        }
    }
    //尾插法的方法。
    public static void InsertNode(Student Head ,Student node){
        Student temp=Head;
        //遍历链表，找到链表末尾。
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=node;
    }
  //头插法的方法。
    public static void InsertNode1(Student Head,Student node){
        node.next = Head;
        Head = node;
        System.out.println("姓名："+Head.name+"  学号："+Head.number+"  爱好："+Head.hobby);
    }
    //中间插入
    public static void InsertNode(Student Head,Student node1,Student node2,Student node3) {
       Student point = Head;
       while((point.number !=  node1.number)& point!=null){
           point = point.next;
       }
       if(point.number==node1.number){
           node3.next = point.next;
           point.next = node3;
       }
    }
    //删除的方法
    public static void DeleteNode(Student Head,Student node){
        Student prenode = Head, Currentnode = Head;
        while(Currentnode != null) {
            if (Currentnode.number != node.number) {
                prenode = Currentnode;
                Currentnode = Currentnode.next;
            } else {
                break;
            }
        }
            prenode.next= Currentnode.next;
    }
}
